<template>
  <div class="dashboard-editor-container">

    <panel-group :dictDevice="dictDevice" :dictBackup="dictBackup" :dictDataCenter="dictDataCenter" @handleSetLineChartData="handleSetLineChartData" />

    <el-row style="background:#fff;padding:16px 16px 0;margin-bottom:32px;">
      <el-col :xs="12" :sm="12" :lg="12" class="card-panel-col">
          <line-chart :chart-data="lineChartData" />
      </el-col>
      <el-col :xs="12" :sm="12" :lg="12" class="card-panel-col">
        <line-chart :chart-data="lineChartDatas" />
      </el-col>
    </el-row>

    <el-row :gutter="32">
      <el-col :xs="24" :sm="24" :lg="12">
        <div class="chart-wrapper">
          <div class="chart-title">
            <p>备份系统运行日志</p>
            <div>
              <el-button type="" plain @click="handleNormal(0)">当前</el-button>
              <el-button type="" plain @click="handleNormal(1)">历史</el-button>
            </div>
          </div>
          <elTables ref="elTabelsRef" :dictList="dictList" :statuIndex="statuIndex" />
        </div>
      </el-col>
      <el-col :xs="24" :sm="24" :lg="12">
        <div class="chart-wrapper">
          <div class="chart-title">
            <p>异常信息提醒</p>
            <div>
              <el-button type="" plain @click="handleAbnormal(0)">当前</el-button>
              <el-button type="" plain @click="handleAbnormal(1)">历史</el-button>
            </div>
          </div>
          <elTables2 ref="elTabels2Ref" :dictList="dictList" :statuIndexAb="statuIndexAb" />
        </div>
      </el-col>

    </el-row>

  </div>
</template>

<script>
import PanelGroup from './components/PanelGroup.vue'
import LineChart from './components/echarts/LineChart'
import RaddarChart from './RaddarChart'
import PieChart from './PieChart'
import BarChart from './BarChart'
import elTables from './components/elTables.vue'
import elTables2 from './components/elTables2.vue'

import {listData} from '@/api/system/dict/data'
import {listUsage} from '@/api/system/usage'
import {listDevice} from '@/api/system/device'

import { 
  getYesterDayDate,
 } from "@/utils/date.js";

const lineChartData = {
  newVisitis: {
    expectedData: [100, 120, 161, 134, 105, 160, 165],
    actualData: [120, 82, 91, 154, 162, 140, 145]
  },
  messages: {
    expectedData: [200, 192, 120, 144, 160, 130, 140],
    actualData: [180, 160, 151, 106, 145, 150, 130]
  },
  purchases: {
    expectedData: [80, 100, 121, 104, 105, 90, 100],
    actualData: [120, 90, 100, 138, 142, 130, 130]
  },
  shoppings: {
    expectedData: [130, 140, 141, 142, 145, 150, 160],
    actualData: [120, 82, 91, 154, 162, 140, 130]
  }
}

export default {
  name: 'Index',
  components: {
    PanelGroup,
    LineChart,
    RaddarChart,
    PieChart,
    BarChart,
    elTables,
    elTables2
  },
  data() {
    return {
      statuIndex: 0,
      statuIndexAb: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        roleId: undefined,
        userName: undefined,
        phonenumber: undefined
      },
      // lineChartData: lineChartData.newVisitis
      lineChartData: {
        expectedData: [],
        actualData: [],
        colors: ['#ff7f50'],
        title: '数据中心',
        name: 'CPU使用率'
      },
      lineChartDatas: {
        expectedData: [],
        actualData: [],
        colors: ['#FFD700'],
        title: '',
        name: '内存使用率'
      },
      dictList: [],
      dictDataCenter: [],
      dictBackup: [],
      dictDevice: [],
    }
  },
  created() {
    this.getDataCenter()
    this.getList()
    this.getListUsage()
    this.getlistDevice()
    this.getDataBackup()
  },
  mounted() {
    this.$refs.elTabelsRef.getCurrentNormalList({
      pageNum: 1,
      pageSize: 10,
      timeFrom: getYesterDayDate(0),
      timeTo: getYesterDayDate(0)
    })
    this.$refs.elTabels2Ref.getCurrentAbnormalList({
      timeFrom: getYesterDayDate(0),
      timeTo: getYesterDayDate(0)
    })
  },
  methods: {
    handleSetLineChartData(type) {
      console.log(type,'-=-=-==-');
      this.lineChartData = lineChartData[type]
    },
    handleNormal(statu) {
      console.log(this,'this.$refs');
      this.statuIndex = statu;
      let params = {
        pageNum: 1,
        pageSize: 10,
        timeFrom: getYesterDayDate(0),
        timeTo: getYesterDayDate(0)
      }
      if(statu == 0) {
        this.$refs.elTabelsRef.getCurrentNormalList(params)
      }else if(statu == 1) {
        this.$refs.elTabelsRef.getNormalList(params)
      }
    },
    handleAbnormal(statu) {
      this.statuIndexAb = statu;
      let params = {
        timeFrom: getYesterDayDate(0),
        timeTo: getYesterDayDate(0)
      }
      if(statu == 0) {
        this.$refs.elTabels2Ref.getCurrentAbnormalList(params)
      }else if(statu == 1) {
        this.$refs.elTabels2Ref.getAbnormalListPanel(params)
      }
    },
    /** 查询字典数据列表 */
    getList() {
      let queryParams = {
        pageNum: 1,
        pageSize: 100,
        dictType: 'status',
      }
      listData(queryParams).then(response => {
        this.dictList = response.rows;
      });
    },
    getDataCenter() {
      let queryParams = {
        pageNum: 1,
        pageSize: 100,
        dictType: 'data_center',
      }
      listData(queryParams).then(response => {
        this.dictDataCenter = response.rows;
      });
    },
    getDataBackup() {
      let queryParams = {
        pageNum: 1,
        pageSize: 100,
        dictType: 'brand',
      }
      listData(queryParams).then(response => {
        this.dictBackup = response.rows;
      });
    },
    getlistDevice() {
      listDevice().then(response => {
        console.log(response,'response');
        this.dictDevice = response.rows.map((item) => {
          return {
            dictValue: item.id,
            dictLabel: item.deviceName
          }
        })
      });
    },
    /** 查询 */
    getListUsage() {
      listUsage(this.queryParams).then(res => {
        // CPU使用率
        this.lineChartData.expectedData = res.rows.map((item) => {
          return item.cpuPercent
        })
        this.lineChartData.actualData = res.rows.map((item) => {
          return item.extend1
        })
        // 内存使用率
        this.lineChartDatas.expectedData = res.rows.map((item) => {
          return item.memoryPercent
        })
        this.lineChartDatas.actualData = res.rows.map((item) => {
          return item.extend1
        })
      });
    },
  }
}
</script>

<style lang="scss" scoped>
.dashboard-editor-container {
  padding: 32px;
  background-color: rgb(240, 242, 245);
  position: relative;

  .chart-wrapper {
    background: #fff;
    padding: 16px 16px 0;
    margin-bottom: 32px;

    .chart-title {
      display: flex;
      justify-content: space-between;
      padding-bottom: 20px;
      p {
        font-size: 20px;
      }
      ::v-deep .el-button + .el-button {
        margin-left: -1px;
      }
    }
  }
}

@media (max-width:1024px) {
  .chart-wrapper {
    padding: 8px;
  }
}
</style>